<template>
  <div>
    <generaltable
      :headerData="headerData"
      :tableData="tableData"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    >
    </generaltable>
  </div>
</template>
<script>
import { shopCartList } from "@/api/shopcart.js";
import generaltable from "@/components/generaltable.vue";
export default {
  name: "shopCart",
  data() {
    return {
      total: 0,
      page: 1,
      limit: 20,
      headerData: [
        {
          prop: "ID",
          label: "用户",
          width: ""
        },
        {
          prop: "title",
          label: "商品",
          width: ""
        }
        ,
        {
          prop: "type",
          label: "商品价格",
          width: ""
        }
      ],
      tableData: []
    };
  },
  mounted() {
    let _self = this;
    _self.shopCartData();
  },
  methods: {
    currentChange(val) {
      let _self = this;
      _self.page = val;
      _self.shopCartData();
    },
    sizeChange(val) {
      let _self = this;
      _self.limit = val;
      _self.shopCartData();
    },
    shopCartData() {
      let _self = this;
      let data = {
        page: _self.page,
        limit: _self.limit
      };
      shopCartList(data).then(res => {
        if (res.code == 20000) {
          _self.tableData = [];
          for (let item of res.data.records) {
            _self.tableData.push({
              ID: item.user.mobile,
              title: item.goods.descript,
              type: _self.priceEdit(item.goods.price),
              page: ""
            });
          }
          _self.total = res.data.total;
        }
      });
    },
    priceEdit(priceNum) {
      let price = "￥" + String(priceNum);
      if (priceNum.length <= 2) {
        return price;
      } else {
        price.substr(0, price.length - 2);
        return (
          price.substr(0, price.length - 2) +
          "." +
          price.substr(price.length - 2, 2)
        );
      }
    }
  },
  components: {
    generaltable
  }
};
</script>
<style scoped></style>
